package com.example.healthapp.ui.fragment;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.healthapp.R;
import com.example.healthapp.ui.activity.NewsDetailsActivity;
import com.example.healthapp.utils.TempData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

// 热点页面的Fragment
public class HotspotFragment extends Fragment {

    View view;
    RecyclerView fgt1_recyclerview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_hotspot, container, false);
        initview();
        refresh();
        event();
        return view;
    }

    @SuppressLint("NotifyDataSetChanged")
    private void refresh() {
        MyRecyclerAdapter myRecyclerAdapter = new MyRecyclerAdapter();
        myRecyclerAdapter.refreshData();//清洗数据
        fgt1_recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        fgt1_recyclerview.setAdapter(myRecyclerAdapter);
        myRecyclerAdapter.notifyDataSetChanged();
    }

    private void event() {
    }

    private void initview() {
        fgt1_recyclerview = view.findViewById(R.id.fgt1_recyclerview);
    }

    //recyclerView列表适配器
    class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
        ArrayList<HashMap<String, Object>> hashMapArrayList;

        public void refreshData() {
            //清洗数据
            hashMapArrayList = new ArrayList<>();
            HashMap<String, Object> hashMap;
            for (int i = 0; i < TempData.data.length * 2; i++) {
                hashMap = new HashMap<>();
                hashMap.put("title", TempData.data[i % TempData.data.length][0]);
                hashMap.put("img", TempData.data[i % TempData.data.length][1]);
                hashMap.put("description", TempData.data[i % TempData.data.length][2]);
                hashMap.put("date", TempData.data[i % TempData.data.length][3]);
                hashMap.put("content", TempData.data[i % TempData.data.length][4]);
                hashMapArrayList.add(hashMap);
            }
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_looknews, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            int index = holder.getAdapterPosition();
            holder.title.setText(hashMapArrayList.get(index).get("title").toString());
            holder.content.setText(hashMapArrayList.get(index).get("content").toString());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");//设置当前时间的格式，为年-月-日
            holder.date.setText(dateFormat.format(new Date()));
            Glide.with(getContext()).load(hashMapArrayList.get(index).get("img").toString()).encodeQuality(10) // 指定压缩质量
                    .override(800, 500).into(holder.imageView);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getContext(), NewsDetailsActivity.class);
                    intent.putExtra("data", hashMapArrayList.get(index));
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return hashMapArrayList.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            TextView title, content, date;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.item_looknews_img);
                title = itemView.findViewById(R.id.item_looknews_title);
                content = itemView.findViewById(R.id.item_looknews_content);
                date = itemView.findViewById(R.id.item_looknews_date);
            }
        }
    }
}